<?php

/**
 * News Album Controller
 * @author Administrator
 */
class AlbumController {
// TODO - Insert your code here
    private $path;
    private $lan;

    /**
     */
    function __construct() {

        $this->path = "../src/module/admin/view_album/temp/";

        if (isset( $_SESSION[SESSION_LANGUAGE])) {
        	$this->lan = $_SESSION[SESSION_LANGUAGE];
        } else {
        	$this->lan = DEFAULT_LANGAUGE;
        }
    }

    /**
     * view table page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewTable() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/table.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_ALBUMS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
        
        // select list
        $db = new DBAccess();
        $list = $db->selectManyByCause( $GLOBALS['albumShortColArray'], ALBUMS, array( ALBUM_LANGUAGE => $this->lan), array( ARTICLE_UPDATETIME => DESC));
        
        // assign item
        foreach ( $list as $item ) {
            $template->assign( ITEM, $item);
            $template->parse( PAGECONTENT . "." . ITEM);
        }
        
        $template->assign( TABLETITLE, "Danh sách albums");
        
        // assign total count and total page
        $count = $db->selectCountRecord( ARTICLES, array(ARTICLE_LANGUAGE => $this->lan));
        $template->assign( TOTALCOUNT, $count);
        $template->assign( TOTALPAGE, 1);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * view form page
     * @return Ambigous <string, unknown_type, boolean>
     */
    public function viewForm() {
        
        // create xTemplate
        $template = new XTemplate( $this->path . $this->lan . "/form.html");
        
        // assign controller and action
        $template->assign( CONTROLLER, PVALUE_CONTROLLER_VIEW_ALBUMS);
        $template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
        
        // get id
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            $db = new DBAccess();
            $item = $db->selectOneByCause( $GLOBALS['albumFullColArray'], ALBUMS, array(ALBUM_ID => $_REQUEST[PARAM_ID]));
            $title = $GLOBALS['formTitleArray'][ALBUMS][EDIT][$this->lan] . $item[ALBUM_TITLE];
        } else {
            $item = array();
            $title = $GLOBALS['formTitleArray'][ALBUMS][ADD][$this->lan];
        }
        
        
        // assign gird title
        $template->assign( FORMTITLE, $title);
        
        // assign form item
        $template->assign( ITEM, $item);
        $template->parse( PAGECONTENT . "." . ITEM);
        
        // return page
        $template->parse( PAGECONTENT);
        return $template->out( PAGECONTENT);
    }

    /**
     * submit form function
     * @return multitype:string
     */
    public function submitForm() {
        
        // reponse array
        $response_array = array();
        
        // get input
        $item = array();
        $item[ALBUM_ID] = $_REQUEST[ALBUM_ID];
        
        $item[ALBUM_TITLE] = $_REQUEST[ALBUM_TITLE];
        $item[ALBUM_IMAGELINK] = $_REQUEST[ALBUM_IMAGELINK];
        
        
        $item[ALBUM_DESCRIPTION] = $_REQUEST[ALBUM_DESCRIPTION];
        
        $item[ALBUM_TITLETAG] = $_REQUEST[ALBUM_TITLETAG];
        $item[ALBUM_URL] = encodeURL( $_REQUEST[ALBUM_URL]);
        $item[ALBUM_METAKEYWORD] = $_REQUEST[ALBUM_METAKEYWORD];
        $item[ALBUM_METADESCRIPTION] = $_REQUEST[ALBUM_METADESCRIPTION];
        
        $item[ALBUM_UPDATETIME] = getCurrentDateTime();
        $item[ALBUM_LANGUAGE] = $this->lan;
        $item[ALBUM_EDITBY] = $_SESSION[SESSION_USERNAME];
        
        if (empty($item[ALBUM_TITLETAG])){
            $item[ALBUM_TITLETAG] = $item[ALBUM_TITLE];
        }
        if (empty($item[ALBUM_URL])){
            $item[ALBUM_URL] = encodeURL( $_REQUEST[ALBUM_TITLE]);
        }
        if (empty($item[ALBUM_METAKEYWORD])){
            $item[ALBUM_METAKEYWORD] = $item[ALBUM_TITLE] . "," .encodeString($item[ALBUM_TITLE]);
        }
        if (empty($item[ALBUM_METADESCRIPTION])){
            $item[ALBUM_METADESCRIPTION] = $item[ALBUM_DESCRIPTION];
        }
        
        // check url
        if (checkURLExist( ALBUMS, $item[ALBUM_URL], $item[ALBUM_ID])) {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = getMessage( MESSAGE_URL_EXISTED);
            
            // return repnose array;
            return $response_array;
        }
        
        // update data to database
        $db = new DBAccess();
        
        if ($item[ALBUM_ID] == 0) {
            if ($db->insertData( $item, ALBUMS)) {
                $new = $db->selectOneByCause( array(ALBUM_ID, ALBUM_URL), ALBUMS, array(ALBUM_URL => $item[ALBUM_URL]));
                updateDataType(ALBUMS, $new[ALBUM_ID], $new[ALBUM_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_ALBUMS . "&act=" . PVALUE_ACTION_VIEW_TABLE;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
            }
        } else {
            if ($db->updateData( $item, ALBUMS)) {
                updateDataType(ALBUMS, $item[ALBUM_ID], $item[ALBUM_URL]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
                $response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_VIEW_ALBUMS . "&act=" . PVALUE_ACTION_VIEW_TABLE;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
            }
        }
        
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete one function
     * @return multitype:
     */
    public function submitDeleteOne() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
            
            $db = new DBAccess();
            
            if ($db->deleteData( ALBUMS, $_REQUEST[PARAM_ID])) {
                deleteDataType(ALBUMS, $_REQUEST[PARAM_ID]);
                $response_array[STATUS] = STATUS_OK;
                $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
            } else {
                $response_array[STATUS] = STATUS_FAIL;
                $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
            }
        } else {
            $response_array[STATUS] = STATUS_FAIL;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     * submit delete many function
     * @return multitype:
     */
    public function submitDeleteMany() {
        
        // reponse array
        $response_array = array();
        
        if (isset( $_REQUEST[PARAM_SELECTED])) {
            
            $IDArray = $_REQUEST[PARAM_SELECTED];
            
            if ($IDArray != null && count( $IDArray) > 0) {
                $delete = true;
                $db = new DBAccess();
                foreach ( $IDArray as $id ) {
                    if ($db->deleteData( ALBUMS, $id)) {
                        deleteDataType(ALBUMS, $id);
                    } else {
                        $delete = false;
                    }
                }
                
                if ($delete) {
                    $response_array[STATUS] = STATUS_OK;
                    $response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
                } else {
                    $response_array[STATUS] = STATUS_FAIL;
                    $response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
                }
            } else {
                $response_array[STATUS] = STATUS_ERROR;
                $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
            }
        } else {
            $response_array[STATUS] = STATUS_ERROR;
            $response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
        }
        
        // return repnose array;
        return $response_array;
    }

    /**
     */
    function __destruct() {
        
        // TODO - Insert your code here
    }
}

?>